home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsnotify.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.4 KB  |  128 lines

  1. /* Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsnotify.c,v 1.2 2000/09/19 19:00:30 lpd Exp $ */
  20. /* Notification machinery implementation */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsstruct.h"
  24. #include "gsnotify.h"
  25.  
  26. /* GC descriptors */
  27. private_st_gs_notify_registration();
  28. public_st_gs_notify_list();
  29.  
  30. /* Initialize a notification list. */
  31. void
  32. gs_notify_init(gs_notify_list_t *nlist, gs_memory_t *mem)
  33. {
  34.     nlist->first = 0;
  35.     nlist->memory = mem;
  36. }
  37.  
  38. /* Register a client. */
  39. int
  40. gs_notify_register(gs_notify_list_t *nlist, gs_notify_proc_t proc,
  41.            void *proc_data)
  42. {
  43.     gs_notify_registration_t *nreg =
  44.     gs_alloc_struct(nlist->memory, gs_notify_registration_t,
  45.             &st_gs_notify_registration, "gs_notify_register");
  46.  
  47.     if (nreg == 0)
  48.     return_error(gs_error_VMerror);
  49.     nreg->proc = proc;
  50.     nreg->proc_data = proc_data;
  51.     nreg->next = nlist->first;
  52.     nlist->first = nreg;
  53.     return 0;
  54. }
  55.  
  56. /*
  57.  * Unregister a client.  Return 1 if the client was registered, 0 if not.
  58.  * If proc_data is 0, unregister all registrations of that proc; otherwise,
  59.  * unregister only the registration of that procedure with that proc_data.
  60.  */
  61. private void
  62. no_unreg_proc(void *pdata)
  63. {
  64. }
  65. int
  66. gs_notify_unregister_calling(gs_notify_list_t *nlist, gs_notify_proc_t proc,
  67.                  void *proc_data,
  68.                  void (*unreg_proc)(P1(void *pdata)))
  69. {
  70.     gs_notify_registration_t **prev = &nlist->first;
  71.     gs_notify_registration_t *cur;
  72.     bool found = 0;
  73.  
  74.     while ((cur = *prev) != 0)
  75.     if (cur->proc == proc &&
  76.         (proc_data == 0 || cur->proc_data == proc_data)
  77.         ) {
  78.         *prev = cur->next;
  79.         unreg_proc(cur->proc_data);
  80.         gs_free_object(nlist->memory, cur, "gs_notify_unregister");
  81.         found = 1;
  82.     } else
  83.         prev = &cur->next;
  84.     return found;
  85. }
  86. int
  87. gs_notify_unregister(gs_notify_list_t *nlist, gs_notify_proc_t proc,
  88.              void *proc_data)
  89. {
  90.     return gs_notify_unregister_calling(nlist, proc, proc_data, no_unreg_proc);
  91. }
  92.  
  93. /*
  94.  * Notify the clients on a list.  If an error occurs, return the first
  95.  * error code, but notify all clients regardless.
  96.  */
  97. int
  98. gs_notify_all(gs_notify_list_t *nlist, void *event_data)
  99. {
  100.     gs_notify_registration_t *cur;
  101.     gs_notify_registration_t *next;
  102.     int ecode = 0;
  103.  
  104.     for (next = nlist->first; (cur = next) != 0;) {
  105.     int code;
  106.  
  107.     next = cur->next;
  108.     code = cur->proc(cur->proc_data, event_data);
  109.     if (code < 0 && ecode == 0)
  110.         ecode = code;
  111.     }
  112.     return ecode;
  113. }
  114.  
  115. /* Release a notification list. */
  116. void
  117. gs_notify_release(gs_notify_list_t *nlist)
  118. {
  119.     gs_memory_t *mem = nlist->memory;
  120.  
  121.     while (nlist->first) {
  122.     gs_notify_registration_t *next = nlist->first->next;
  123.  
  124.     gs_free_object(mem, nlist->first, "gs_notify_release");
  125.     nlist->first = next;
  126.     }
  127. }
  128.